package com.so206.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.so206.mapper.SystemDocMapper;
import com.so206.po.SystemDoc;
import com.so206.service.ApiDocService;
import com.so206.utils.BeanCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ApiDocServiceImpl implements ApiDocService {

    @Autowired
    private SystemDocMapper mapper;

    @Override
    public void saveDocs(SystemDoc doc) {
        doc.setCreateTime(new Date());
        mapper.insert(doc);
    }

    @Override
    public SystemDoc findDocById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateDocById(SystemDoc doc) {
        SystemDoc model = mapper.selectByPrimaryKey(doc.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(doc, model);
            mapper.updateByPrimaryKeySelective(model);
        }
    }

    @Override
    public void deleteDocById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<SystemDoc> findDocByPage(Integer page, Integer rows, Integer status) {
        PageHelper.startPage(page, rows);
        List<SystemDoc> list = mapper.selectByStatus(status);
        return  new PageInfo<>(list);
    }

    @Override
    public List<SystemDoc> findAllDisplayDoc(Integer status) {
        return mapper.selectByStatus(status);
    }


}
